package com.xiao.androidcompose01.screen

import android.os.Parcel
import android.os.Parcelable
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.LocationOn
import androidx.compose.material3.Icon
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.graphics.vector.rememberVectorPainter
import androidx.compose.ui.tooling.preview.Preview
import cafe.adriel.voyager.core.screen.Screen
import cafe.adriel.voyager.koin.koinScreenModel
import cafe.adriel.voyager.navigator.tab.CurrentTab
import cafe.adriel.voyager.navigator.tab.LocalTabNavigator
import cafe.adriel.voyager.navigator.tab.Tab
import cafe.adriel.voyager.navigator.tab.TabNavigator
import com.xiao.androidcompose01.LocalAccountData
import com.xiao.androidcompose01.api.data.AccountData
import com.xiao.androidcompose01.screen.main.HomeTab
import com.xiao.androidcompose01.screen.main.MapTab
import com.xiao.androidcompose01.screen.main.MineTab
import com.xiao.androidcompose01.ui.theme.MainColor
import com.xiao.androidcompose01.weight.click

class MainScreen(private val accountData: AccountData) : Screen{
   @Composable
   override fun Content() {
      val homeTab = HomeTab()
      val mapTab = MapTab()
      val mineTab = MineTab()
      CompositionLocalProvider(LocalAccountData provides accountData) {
         TabNavigator(homeTab){
            Scaffold(
               modifier = Modifier.fillMaxSize(),
               content = { paddingValues ->
                  Column(modifier = Modifier
                     .fillMaxSize()
                     .padding(bottom = paddingValues.calculateBottomPadding())) {
                     Column(modifier = Modifier
                        .fillMaxWidth()
                        .weight(1f)) {
                        CurrentTab()
                     }
                     Row(modifier = Modifier.fillMaxWidth()) {
                        TabNavigationItem(homeTab)
                        TabNavigationItem(mapTab)
                        TabNavigationItem(mineTab)
                     }
                  }
                  
                  
               }
            )
         }
      }
      
   }
   
}

@Composable
private fun RowScope.TabNavigationItem(tab: Tab) {
   val tabNavigator = LocalTabNavigator.current
   BottomNavigationItem(
      modifier = Modifier.weight(1f),
      selected = tabNavigator.current == tab,
      onClick = {
         tabNavigator.current = tab
                },
      title = tab.options.title,
      icon = tab.options.icon ?: rememberVectorPainter(Icons.Default.Home)
   )
}

@Composable
fun BottomNavigationItem(modifier: Modifier,selected: Boolean, title:String, icon: Painter, onClick: () -> Unit) {
   val color = if (selected) MainColor else Color.Black
   Column(modifier= modifier
      .fillMaxWidth()
      .background(Color.White)
      .click(true, onClick), horizontalAlignment = Alignment.CenterHorizontally) {
      Icon(icon, contentDescription = title, tint = color)
      Text(text = title, color = color)
   }
}

@Preview
@Composable
private fun BottomNavigationItemPreview() {
   Row(modifier = Modifier.fillMaxWidth()) {
      BottomNavigationItem(Modifier.weight(1f), true, "房间",
         rememberVectorPainter(Icons.Default.Home), {})
      BottomNavigationItem(Modifier.weight(1f), false, "地图",
         rememberVectorPainter(Icons.Filled.LocationOn), {})
      BottomNavigationItem(Modifier.weight(1f), false, "我的",
         rememberVectorPainter(Icons.Default.Home), {})
   }
}